

import '../base/base_operation.dart';
import '../effect_template.dart';

enum FixType { contain, fill, cover }

extension FixTypeExt on FixType {
  String get text {
    switch (this) {
      case FixType.contain:
        return '适应';
      case FixType.fill:
        return '拉伸';
      case FixType.cover:
        return '裁剪';
    }
  }
}

class ImageOperation implements BaseOperation {
  late List<String> imagePaths;
  List<String>? thumbImagePaths;
  late int brightness;
  late int width;
  late int height;
  late FixType fixType;
  SpeedOrientation? speedOrientation;
  int? smallWidth;
  int? smallHeight;
  @override
  int? speed;

  ImageOperation({
    required this.imagePaths,
    this.thumbImagePaths,
    this.brightness = 100,
    this.speed = 80,
    required this.width,
    required this.height,
    required this.fixType,
    this.speedOrientation,
    this.smallWidth,
    this.smallHeight,
  });

  ImageOperation copyWith({
    int? brightness,
    int? speed,
    FixType? fixType,
    SpeedOrientation? speedOrientation
  }) {
    return ImageOperation(
        brightness: brightness ?? this.brightness,
        speed: speed ?? this.speed,
        fixType: fixType ?? this.fixType,
        imagePaths: this.imagePaths,
        thumbImagePaths: this.thumbImagePaths,
        width: this.width,
        height: this.height,
        speedOrientation: speedOrientation);
  }

  ImageOperation.fromJson(Map<String, dynamic> json) {
    imagePaths = json['imagePaths'].cast<String>();
    if (json['thumbImagePaths'] != null) {
      thumbImagePaths = json['thumbImagePaths'].cast<String>();
    }
    brightness = json['brightness']??100;
    speed = json['speed']??0;
    width = json['width'];
    height = json['height'];
    fixType = FixType.values[json['fixType'] ?? 1];
    if(json['speedOrientation'] != null){
      speedOrientation = SpeedOrientation.values[json['speedOrientation']];
    }
    if(json['smallWidth'] != null){
      smallWidth = json['smallWidth'];
    }
    if(json['smallHeight'] != null){
      smallHeight = json['smallHeight'];
    }
  }

  @override
  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['imagePaths'] = this.imagePaths;
    data['thumbImagePaths'] = this.thumbImagePaths;
    data['brightness'] = this.brightness;
    data['speed'] = this.speed;
    data['width'] = this.width;
    data['height'] = this.height;
    data['fixType'] = this.fixType.index;
    data['speedOrientation'] = speedOrientation?.index;
    data['smallWidth'] = this.smallWidth;
    data['smallHeight'] = this.smallHeight;
    return data;
  }
}
